<?php

namespace frontend\controllers;

use common\models\CepingDetail;
use common\models\CepingPaper;
use Yii;
use common\models\CepingSubject;
use common\models\search\CepingSubjectSearch;
use yii\data\Pagination;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * CsubjectController implements the CRUD actions for CepingSubject model.
 */
class CsubjectController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }
    public function actionTest() {
        $paperId=CepingPaper::find()->orderBy('weight DESC')->one()->paperId;
        $question = CepingSubject::find()->where(['rank'=>'1','paperId'=>$paperId,'status'=>1])->one();
        $count=CepingSubject::find()->where(['parent_id'=>null,'status'=>1,'paperId'=>$paperId])->count();//->where(['status'=>1])->all()
      //p($question);die;
        $answers = CepingSubject::find()->where(['parent_id'=>$question->label_id,'status'=>1,'paperId'=>$paperId])->all();
     //   p($answers);die;
        $times=rand(100,999).date('md',time()).rand(100,999);
        return $this->render('test',[
            'question'=> $question,
            'answers'=>$answers,
            'count'=>$count,
            'paperId'=>$paperId,
            'times'=>$times
        ]);
    }
    public function actionPrevious($paperId,$times) {
        $uid=Yii::$app->user->id;
      //  $time=time();
        $record = CepingDetail::find()->where(['userId'=>$uid,'paperId'=>$paperId,'status'=>1,'times'=>$times])->orderBy('updated_at DESC')->one();
      //  p($record);die;
        $record->status=0;
        $record->save();


        //detail表中subjectId为 题序号
        $question=CepingSubject::find()->where(['rank'=>$record->subjectId,'paperId'=>$paperId])->one();
       // p($record);die;
        $answers = CepingSubject::find()->where(['parent_id'=>$question->label_id,'paperId'=>$paperId])->all();
        return \Yii::createObject([
            'class' => 'yii\web\Response',
            'format' => \yii\web\Response::FORMAT_JSON,
            'data' => [
                'question' => $question,
                'answers' => $answers
            ],
        ]);
    }
    public function actionNext($subjectId, $answerId,$paperId,$times) {   //题目和选项的label_id，试卷号

    //    p($subjectId);p($answerId);p($paperId);die;

        //create detail
     //   $userId = 110103;
        $userId = Yii::$app->user->id;
//        $label = $answer->label;

        if($detail=CepingDetail::find()->where(['userId'=>$userId,'subjectId'=>$subjectId,'paperId'=>$paperId,'times'=>$times])->one()){
            $detail->label = $answerId;
            $detail->status = 1;
            $detailSaveResult =$detail->save();
        }else{
            $detail = new CepingDetail();
            $detail->userId = $userId;
            $detail->paperId = $paperId;
            $detail->subjectId = $subjectId;    //detail表中subjectId为 题序号
            $detail->times = $times;
            $detail->label = $answerId;
            $detail->status = 1;
            $detailSaveResult = $detail->save();
        }

        //通过选择的答案编号 和 试卷编号 找到此条label
        $answer = CepingSubject::find()->where(['label_id'=>$answerId,'paperId'=>$paperId])->one();
        //通过此条label的parent_id属性找到 父标签对象
        $parent=CepingSubject::find()->where(['label_id'=>$answer->parent_id,'paperId'=>$paperId])->one();

      //  p($parent);die;
        if($detailSaveResult) {
            if(empty($answer->foreign_id)) {
                //如果此条答案没有跳转 则使用父对象rank+1 作为跳转编号
                $answer->foreign_id = $parent->rank*1 + 1;
                //    p($answer->foreign_id);
                //如果有foreign_id 则通过foreign_id跳转id找到下道题的对象
                    $next = CepingSubject::find()->where(['rank'=>$answer->foreign_id,'paperId'=>$paperId])->one();
            }else   $next = CepingSubject::find()->where(['rank'=>$answer->foreign_id,'paperId'=>$paperId])->one();

       //    p($next);
            //通过下道题的对象编号 找到此题的选项
            $nextAnswer = CepingSubject::find()->where(['parent_id'=>$next->label_id,'paperId'=>$paperId])->all();
        // p($nextAnswer);die; //
            $sub_next = CepingSubject::find()->where(['rank'=>($next->rank + 1),'paperId'=>$paperId])->one();

            if($sub_next) {
                $end = false;
            } else {
                $end = true;
            }
            return \Yii::createObject([
                'class' => 'yii\web\Response',
                'format' => \yii\web\Response::FORMAT_JSON,
                'data' => [
                    'next' => $next,
                    'nextAnswer' => $nextAnswer,
                    'end' => $end
                ],
            ]);
        }
        else return false;
    }
    public function actionResult($subjectId, $answerId, $paperId,$times){

        $userId = Yii::$app->user->id;

            $detail = new CepingDetail();
            $detail->userId = $userId;
            $detail->paperId = $paperId;
            $detail->subjectId = $subjectId;
            $detail->times = $times;
            $detail->label = $answerId;
            $detail->status = 1;
            $detail->save();

        return $this->redirect(['creport/init', 'paperId'=>$paperId,'times'=>$times]);
    }



}
